--- 模块功能：批量设置设备参数
-- @module batchSetParameters
-- @author zhangfengfe
-- @license MIT
-- @copyright OpenLuat.com
-- @release 2024.05.07
module(..., package.seeall)
require "sys"
require "nvm"
require "config"
-- 参数配置文件初始化
nvm.init("config.lua", true)
-- 批量参数设置归属分类
local function belongToWhich(nameOfParam)
    local belong = ""
    if ("centerip license owner type groupId clientId uname pwd scope mqtthost topic eqrTopic answerTopic port ftpEnable ftpHost ftpPort ftpName ftpPsw contentType enableVerify authServer mckParams caParams"):match(
        nameOfParam) ~= nil then
        belong = "config"
    elseif ("clientNo city typeId orgId"):match(nameOfParam) ~= nil then
        belong = "info"
    else
        belong = "other"
    end
    return belong
end
-- 批量设置设备参数
function SetParamBatch(paramName, paramValue)
    log.info("paramName:", paramName, 'paramValue', paramValue)
    if paramName == "" then
        if paramValue == "" then
            sys.restart("设备批量重启命令执行成功！！")
        end
    else
        local belongFlag = belongToWhich(paramName)
        log.info("belongFlag:", belongFlag)
        if belongFlag == "other" then
            if paramName == "appName" then
                if paramValue ~= nvm.get("appName") then
                    nvm.set("appName", paramValue)
                end
            elseif paramName == "longitude" then
                if paramValue ~= nvm.get("longitude") then
                    nvm.set("longitude", paramValue)
                end
            elseif paramName == "latitude" then
                if paramValue ~= nvm.get("latitude") then
                    nvm.set("latitude", paramValue)
                end
            elseif paramName == "cloud" then
                if paramValue ~= nvm.get("cloud") then
                    nvm.set("cloud", paramValue)
                end
            elseif paramName == "lock" then
                if paramValue ~= nvm.get("lock") then
                    nvm.set("lock", paramValue)
                end
            elseif paramName == "openTime" then
                if paramValue ~= nvm.get("openTime") then
                    nvm.set("openTime", paramValue)
                end
            elseif paramName == "closeTime" then
                if paramValue ~= nvm.get("closeTime") then
                    nvm.set("closeTime", paramValue)
                end
            elseif paramName == "updateUrl" then
                if paramValue ~= nvm.get("updateUrl") then
                    nvm.set("updateUrl", paramValue)
                end
            elseif paramName == "versionUrl" then
                if paramValue ~= nvm.get("versionUrl") then
                    nvm.set("versionUrl", paramValue)
                end
            elseif paramName == "broker" then
                if paramValue ~= nvm.get("broker") then
                    nvm.set("broker", paramValue)
                end
            elseif paramName == "ntpServer" then
                if paramValue ~= nvm.get("ntpServer") then
                    nvm.set("ntpServer", paramValue)
                end
            elseif paramName == "delayTime" then
                if paramValue ~= nvm.get("delayTime") then
                    nvm.set("delayTime", paramValue)
                end
            elseif paramName == "centerName" then
                if paramValue ~= nvm.get("centerName") then
                    nvm.set("centerName", paramValue)
                end
            elseif paramName == "intensity" then
                if paramValue ~= nvm.get("intensity") then
                    nvm.set("intensity", paramValue)
                end
            elseif paramName == "magnitude" then
                if paramValue ~= nvm.get("magnitude") then
                    nvm.set("magnitude", paramValue)
                end
            elseif paramName == "centerIntensity" then
                if paramValue ~= nvm.get("centerIntensity") then
                    nvm.set("centerIntensity", paramValue)
                end
            elseif paramName == "countDown" then
                if paramValue ~= nvm.get("countDown") then
                    nvm.set("countDown", paramValue)
                end
            elseif paramName == "alertSound" then
                if paramValue ~= nvm.get("alertSound") then
                    nvm.set("alertSound", paramValue)
                end
            elseif paramName == "alertLight" then
                if paramValue ~= nvm.get("alertLight") then
                    nvm.set("alertLight", paramValue)
                end
            elseif paramName == "emergencyLamp" then
                if paramValue ~= nvm.get("emergencyLamp") then
                    nvm.set("emergencyLamp", paramValue)
                end
            elseif paramName == "relay" then
                if paramValue ~= nvm.get("relay") then
                    nvm.set("relay", paramValue)
                end
            elseif paramName == "adVolume" then
                if paramValue ~= nvm.get("adVolume") then
                    nvm.set("adVolume", paramValue)
                end
            elseif paramName == "alertVolume" then
                if paramValue ~= nvm.get("alertVolume") then
                    nvm.set("alertVolume", paramValue)
                end
            elseif paramName == "intensityReturn" then
                if paramValue ~= nvm.get("intensityReturn") then
                    nvm.set("intensityReturn", paramValue)
                end
            elseif paramName == "adUpdateUrl" then
                if paramValue ~= nvm.get("adUpdateUrl") then
                    nvm.set("adUpdateUrl", paramValue)
                end
            elseif paramName == "orgName" then
                if paramValue ~= nvm.get("orgName") then
                    nvm.set("orgName", paramValue)
                end
            elseif paramName == "makeVideo" then
                if paramValue ~= nvm.get("makeVideo") then
                    nvm.set("makeVideo", paramValue)
                end
            elseif paramName == "uploadVideoUrl" then
                if paramValue ~= nvm.get("uploadVideoUrl") then
                    nvm.set("uploadVideoUrl", paramValue)
                end
            elseif paramName == "broadcast" then
                if paramValue ~= nvm.get("broadcast") then
                    nvm.set("broadcast", paramValue)
                end
            elseif paramName == "broadcastUrl" then
                if paramValue ~= nvm.get("broadcastUrl") then
                    nvm.set("broadcastUrl", paramValue)
                end
            elseif paramName == "h24" then
                if paramValue ~= nvm.get("h24") then
                    nvm.set("h24", paramValue)
                end
            elseif paramName == "enable433" then
                if paramValue ~= nvm.get("enable433") then
                    nvm.set("enable433", paramValue)
                end
            elseif paramName == "autoUpdateLog" then
                if paramValue ~= nvm.get("autoUpdateLog") then
                    nvm.set("autoUpdateLog", paramValue)
                end
            elseif paramName == "alertDisplayMode" then
                if paramValue ~= nvm.get("alertDisplayMode") then
                    nvm.set("alertDisplayMode", paramValue)
                end
            elseif paramName == "color1" then
                if paramValue ~= nvm.get("color1") then
                    nvm.set("color1", paramValue)
                end
            elseif paramName == "color2" then
                if paramValue ~= nvm.get("color2") then
                    nvm.set("color2", paramValue)
                end
            elseif paramName == "color3" then
                if paramValue ~= nvm.get("color3") then
                    nvm.set("color3", paramValue)
                end
            elseif paramName == "color4" then
                if paramValue ~= nvm.get("color4") then
                    nvm.set("color4", paramValue)
                end
            elseif paramName == "luminance" then
                if paramValue ~= nvm.get("luminance") then
                    nvm.set("luminance", paramValue)
                end
            elseif paramName == "hz" then
                if paramValue ~= nvm.get("hz") then
                    nvm.set("hz", paramValue)
                end

            elseif paramName == "autoCheckTime" then
                if paramValue ~= nvm.get("autoCheckTime") then
                    nvm.set("autoCheckTime", paramValue)
                end
            elseif paramName == "regCenter" then
                if paramValue ~= nvm.get("regCenter") then
                    nvm.set("regCenter", paramValue)
                end
            elseif paramName == "lockPsw" then
                if paramValue ~= nvm.get("lockPsw") then
                    nvm.set("lockPsw", paramValue)
                end
            elseif paramName == "receive" then
                if paramValue ~= nvm.get("receive") then
                    nvm.set("receive", paramValue)
                end
            elseif paramName == "replayLockPsw" then
                if paramValue ~= nvm.get("replayLockPsw") then
                    nvm.set("replayLockPsw", paramValue)
                end
            elseif paramName == "enableRF433" then
                if paramValue ~= nvm.get("enableRF433") then
                    nvm.set("enableRF433", paramValue)
                end
            elseif paramName == "enableAcc" then
                if paramValue ~= nvm.get("enableAcc") then
                    nvm.set("enableAcc", paramValue)
                end
            elseif paramName == "enableTF" then
                if paramValue ~= nvm.get("enableTF") then
                    nvm.set("enableTF", paramValue)
                end
            elseif paramName == "enableHandset" then
                if paramValue ~= nvm.get("enableHandset") then
                    nvm.set("enableHandset", paramValue)
                end
            elseif paramName == "hideBattery" then
                if paramValue ~= nvm.get("hideBattery") then
                    nvm.set("hideBattery", paramValue)
                end
            elseif paramName == "enableRealKey" then
                if paramValue ~= nvm.get("enableRealKey") then
                    nvm.set("enableRealKey", paramValue)
                end
            elseif paramName == "brightnessPositive" then
                if paramValue ~= nvm.get("brightnessPositive") then
                    nvm.set("brightnessPositive", paramValue)
                end
            elseif paramName == "clientName" then
                if paramValue ~= nvm.get("clientName") then
                    nvm.set("clientName", paramValue)
                end
            elseif paramName == "updateRate" then
                if paramValue ~= nvm.get("updateRate") then
                    nvm.set("updateRate", paramValue)
                end
            elseif paramName == "memsIp" then
                if paramValue ~= nvm.get("memsIp") then
                    nvm.set("memsIp", paramValue)
                end
            elseif paramName == "stations" then
                if paramValue ~= nvm.get("stations") then
                    nvm.set("stations", paramValue)
                end
            elseif paramName == "move" then
                if paramValue ~= nvm.get("move") then
                    nvm.set("move", paramValue)
                end
            elseif paramName == "rotate" then
                if paramValue ~= nvm.get("rotate") then
                    nvm.set("rotate", paramValue)
                end
            elseif paramName == "blueLimit" then
                if paramValue ~= nvm.get("blueLimit") then
                    nvm.set("blueLimit", paramValue)
                end
            elseif paramName == "yellowLimit" then
                if paramValue ~= nvm.get("yellowLimit") then
                    nvm.set("yellowLimit", paramValue)
                end
            elseif paramName == "orangeLimit" then
                if paramValue ~= nvm.get("orangeLimit") then
                    nvm.set("orangeLimit", paramValue)
                end
            elseif paramName == "redLimit" then
                if paramValue ~= nvm.get("redLimit") then
                    nvm.set("redLimit", paramValue)
                end
            elseif paramName == "afterSecond" then
                if paramValue ~= nvm.get("afterSecond") then
                    nvm.set("afterSecond", paramValue)
                end
            elseif paramName == "sourceText" then
                if paramValue ~= nvm.get("sourceText") then
                    nvm.set("sourceText", paramValue)
                end
            elseif paramName == "blueGuide" then
                if paramValue ~= nvm.get("blueGuide") then
                    nvm.set("blueGuide", paramValue)
                end
            elseif paramName == "yellowGuide" then
                if paramValue ~= nvm.get("yellowGuide") then
                    nvm.set("yellowGuide", paramValue)
                end
            elseif paramName == "orangeGuide" then
                if paramValue ~= nvm.get("orangeGuide") then
                    nvm.set("orangeGuide", paramValue)
                end
            elseif paramName == "redGuide" then
                if paramValue ~= nvm.get("redGuide") then
                    nvm.set("redGuide", paramValue)
                end
            elseif paramName == "exerciseText" then
                if paramValue ~= nvm.get("exerciseText") then
                    nvm.set("exerciseText", paramValue)
                end
            elseif paramName == "enableMax10" then
                if paramValue ~= nvm.get("enableMax10") then
                    nvm.set("enableMax10", paramValue)
                end
            elseif paramName == "cancelCount" then
                if paramValue ~= nvm.get("cancelCount") then
                    nvm.set("cancelCount", paramValue)
                end
            elseif paramName == "fastCount" then
                if paramValue ~= nvm.get("fastCount") then
                    nvm.set("fastCount", paramValue)
                end
            elseif paramName == "carouselCount" then
                if paramValue ~= nvm.get("carouselCount") then
                    nvm.set("carouselCount", paramValue)
                end
            elseif paramName == "master" then
                if paramValue ~= nvm.get("master") then
                    nvm.set("master", paramValue)
                end
            elseif paramName == "masterPair" then
                if paramValue ~= nvm.get("masterPair") then
                    nvm.set("masterPair", paramValue)
                end
            end

        elseif belongFlag == "info" then
            if paramName == "city" then
                nvm.sett("info", 2, paramValue, "SVR")
            elseif paramName == "typeId" then
                nvm.sett("info", 3, paramValue, "SVR")
            elseif paramName == "orgId" then
                nvm.sett("info", 4, paramValue, "SVR")
            end
        elseif belongFlag == "config" then
            if paramName == "centerip" then -- 注册中心地址
                nvm.sett("centerConfig", 1, paramValue, "SVR")
            elseif paramName == "license" then -- 注册中心license
                nvm.sett("centerConfig", 2, paramValue, "SVR")
            elseif paramName == "owner" then -- 注册中心所属机构
                nvm.sett("centerConfig", 3, paramValue, "SVR")
            elseif paramName == "type" then -- 注册中心终端类型
                log.info("type:", paramName, "paramValue:", paramValue)
                nvm.sett("centerConfig", 4, paramValue, "SVR")
            elseif paramName == "groupId" then -- 注册中心分组
                nvm.sett("centerConfig", 5, paramValue, "SVR")
            elseif paramName == "clientId" then -- 注册中心客户端Id
                nvm.sett("centerConfig", 6, paramValue, "SVR")
            elseif paramName == "uname" then -- 注册中心用户名
                nvm.sett("centerConfig", 7, paramValue, "SVR")
            elseif paramName == "pwd" then -- 注册中心密码
                nvm.sett("centerConfig", 8, paramValue, "SVR")
            elseif paramName == "scope" then -- 注册中心范围
                nvm.sett("centerConfig", 9, paramValue, "SVR")
            elseif paramName == "mqtthost" then -- 注册中心mq地址
                nvm.sett("centerConfig", 10, paramValue, "SVR")
            elseif paramName == "topic" then -- 注册中心预警主题
                nvm.sett("centerConfig", 11, paramValue, "SVR")
            elseif paramName == "eqrTopic" then -- 注册中心速报主题
                nvm.sett("centerConfig", 12, paramValue, "SVR")
            elseif paramName == "answerTopic" then -- 注册中心应答主题
                nvm.sett("centerConfig", 13, paramValue, "SVR")
            elseif paramName == "port" then -- 注册中心mq端口
                nvm.sett("centerConfig", 14, paramValue, "SVR")
            elseif paramName == "ftpEnable" then -- 注册中心是否启用FTP
                nvm.sett("centerConfig", 15, paramValue, "SVR")
            elseif paramName == "ftpHost" then -- 注册中心FTP地址
                nvm.sett("centerConfig", 16, paramValue, "SVR")
            elseif paramName == "ftpPort" then -- 注册中心FTP端口
                nvm.sett("centerConfig", 17, paramValue, "SVR")
            elseif paramName == "ftpName" then -- 注册中心FTP用户名
                nvm.sett("centerConfig", 18, paramValue, "SVR")
            elseif paramName == "ftpPsw" then -- 注册中心FTP密码
                nvm.sett("centerConfig", 19, paramValue, "SVR")
            elseif paramName == "contentType" then -- 内容类型
                nvm.sett("centerConfig", 20, paramValue, "SVR")
            elseif paramName == "enableVerify" then -- 注册中心启用国密验签
                nvm.sett("centerConfig", 21, paramValue, "SVR")
            elseif paramName == "authServer" then -- 注册中心认证服务器地址
                nvm.sett("centerConfig", 22, paramValue, "SVR")
            elseif paramName == "mckParams" then -- 注册中心介质云参数
                nvm.sett("centerConfig", 23, paramValue, "SVR")
            elseif paramName == "caParams" then -- 注册中心证书参数
                nvm.sett("centerConfig", 24, paramValue, "SVR")
            end
        end
        sys.restart("远程批量参数设置，重启使之生效！")
    end
end

-- 批量重置注册中心参数
function BatchResetcenterConfigParam()
    nvm.sett("centerConfig", 1, "", "SVR")
    nvm.sett("centerConfig", 2, "", "SVR")
    nvm.sett("centerConfig", 3, "", "SVR")
    nvm.sett("centerConfig", 4, "", "SVR")
    nvm.sett("centerConfig", 5, "", "SVR")
    nvm.sett("centerConfig", 6, "", "SVR")
    nvm.sett("centerConfig", 7, "", "SVR")
    nvm.sett("centerConfig", 8, "", "SVR")
    nvm.sett("centerConfig", 9, "", "SVR")
    nvm.sett("centerConfig", 10, "", "SVR")
    nvm.sett("centerConfig", 11, "", "SVR")
    nvm.sett("centerConfig", 12, "", "SVR")
    nvm.sett("centerConfig", 13, "", "SVR")
    nvm.sett("centerConfig", 14, "", "SVR")
    nvm.sett("centerConfig", 15, "false", "SVR")
    nvm.sett("centerConfig", 16, "", "SVR")
    nvm.sett("centerConfig", 17, "", "SVR")
    nvm.sett("centerConfig", 18, "", "SVR")
    nvm.sett("centerConfig", 19, "", "SVR")
    nvm.sett("centerConfig", 20, "", "SVR")
    nvm.sett("centerConfig", 21, "false", "SVR")
    nvm.sett("centerConfig", 22, "", "SVR")
    sys.restart("注册参数重置，设备重启！")
end
